package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import language.Strings;

import org.jdom.JDOMException;

import util.Constantes;
import util.PersistenciaXml;
import bean.Preference;

public class StartPreferences extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lbIdioma = null;
	private JRadioButton rbEN = null;
	private JRadioButton rbPtBr = null;
	private JButton btSalvar = null;

	private JPanel jPanelTitulo = null;
	private JLabel lbOperacaoTitulo = null;
	
	public StartPreferences() {
		super();
		initialize();
		
		carregaDadosIniciais();
	}

	private void initialize() {
		this.setSize(414, 300);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TITULO_PREFERENCES);
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				Principal.JANELA_ABERTA = false;				
			}			
		});
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbIdioma = new JLabel();
			lbIdioma.setBounds(new Rectangle(20, 102, 111, 24));
			lbIdioma.setText(Strings.TEXT_LABEL_IDIOMA);
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lbIdioma, null);
			jContentPane.add(getRbEN(), null);
			jContentPane.add(getRbPtBr(), null);
			jContentPane.add(getBtSalvar(), null);
						
			jContentPane.add(getJPanelTitulo(), null);
			jContentPane.setBackground(Color.WHITE);
			
			createButtonGroup();
		}
		return jContentPane;
	}

	private JPanel getJPanelTitulo() {
		if (jPanelTitulo == null) {
			jPanelTitulo = new JPanel();
			jPanelTitulo.setLayout(null);
			jPanelTitulo.setBounds(new Rectangle(0, 0, 420, 86));
			lbOperacaoTitulo = new JLabel();
			lbOperacaoTitulo.setText("Preferences");
			lbOperacaoTitulo.setBounds(new Rectangle(50, 25, 132, 25));
			lbOperacaoTitulo.setFont(new Font("Arial", Font.BOLD, 18));
			lbOperacaoTitulo.setForeground(Color.WHITE);
			jPanelTitulo.add(lbOperacaoTitulo, null);
			jPanelTitulo.setBackground(new Color(35,35,142));
		}
		return jPanelTitulo;
	}
	
	private JRadioButton getRbEN() {
		if (rbEN == null) {
			rbEN = new JRadioButton();
			rbEN.setBounds(new Rectangle(35, 140, 110, 21));
			rbEN.setText("EN");			
			rbEN.setBackground(Color.WHITE);
		}
		return rbEN;
	}

	private JRadioButton getRbPtBr() {
		if (rbPtBr == null) {
			rbPtBr = new JRadioButton();
			rbPtBr.setBounds(new Rectangle(210, 140, 90, 21));
			rbPtBr.setText("PtBr");
			rbPtBr.setBackground(Color.WHITE);
		}
		return rbPtBr;
	}

	private JButton getBtSalvar() {
		if (btSalvar == null) {
			btSalvar = new JButton();
			btSalvar.setBounds(new Rectangle(261, 218, 110, 25));
			btSalvar.setText(Strings.TEXT_BT_SALVAR);
			btSalvar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					eventoSalvar();
				}
			});
		}
		return btSalvar;
	}

	private void createButtonGroup(){
		ButtonGroup group = new ButtonGroup();
		group.add(getRbEN());
		group.add(getRbPtBr());
	}

	private void eventoSalvar() {
		Preference preference = new Preference();
		
		if(getRbEN().isSelected()){
			preference.setIdioma(Constantes.IDIOMA_EN);
		} else if(getRbPtBr().isSelected()){
			preference.setIdioma(Constantes.IDIOMA_PT_BT);
		}
		
		
		PersistenciaXml persistenciaXml = new PersistenciaXml();
		persistenciaXml.salvaPreferences(preference);
		
		Strings.selecionaIdioma(preference.getIdioma());
	}
	
	private void carregaDadosIniciais(){
		try {
			if(Constantes.PREFERENCES_XML.exists()){
				PersistenciaXml persistenciaXml = new PersistenciaXml();
				Preference preference = persistenciaXml.selectPreferencesXML();
				
				if(preference.getIdioma().equals(Constantes.IDIOMA_EN)){
					getRbEN().setSelected(true);
				} else if(preference.getIdioma().equals(Constantes.IDIOMA_PT_BT)){
					getRbPtBr().setSelected(true);
				}				
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
